/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.deange.ropeprogress;

import com.deange.ropeprogressview.RopeProgressBar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;

import java.util.regex.Pattern;

/**
 * BaseAbility父类
 *
 * @author hw
 * @since 2021/5/29
 */
public abstract class BaseAbility
        extends AbilitySlice
        implements
        Slider.ValueChangedListener,
        Component.ClickedListener {
    private Slider mSlider;
    private TextField mEditText;
    private RopeProgressBar mRopeProgressBar;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        Component component = LayoutScatter.getInstance(this).parse(getLayoutId(), null, false);
        ComponentContainer decorView = (ComponentContainer) component;
        setUIContent(decorView);
        mRopeProgressBar = findRopeProgressView(decorView);
        if (findComponentById(ResourceTable.Id_progress_slider) instanceof Slider) {
            mSlider = (Slider) findComponentById(ResourceTable.Id_progress_slider);
            mSlider.setValueChangedListener(this);
            mSlider.getThumbElement().setBounds(0, 0, 30, 30);
        }
        findComponentById(ResourceTable.Id_button_animate).setClickedListener(this);
        if (findComponentById(ResourceTable.Id_progress_jump) instanceof TextField) {
            mEditText = (TextField) findComponentById(ResourceTable.Id_progress_jump);
            mEditText.addTextObserver(new Text.TextObserver() {
                @Override
                public void onTextUpdated(String text, int start, int oldLength, int addLength) {
                    mEditText.setText(text.replaceAll("\\D", ""));
                }
            });
        }
    }

    /**
     * RopeProgressBar
     *
     * @return RopeProgressBar
     */
    protected RopeProgressBar getRopeProgressBar() {
        return mRopeProgressBar;
    }

    private RopeProgressBar findRopeProgressView(final Component view) {
        if (view instanceof RopeProgressBar) {
            return (RopeProgressBar) view;
        } else {
            if (view instanceof ComponentContainer) {
                ComponentContainer parent = ((ComponentContainer) view);
                RopeProgressBar child;
                for (int ips = 0; ips < parent.getChildCount(); ips++) {
                    if ((child = findRopeProgressView(parent.getComponentAt(ips))) != null) {
                        return child;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public void onProgressUpdated(final Slider seekBar, final int progress, final boolean user) {
        mRopeProgressBar.setProgress(progress);
    }

    @Override
    public void onTouchStart(final Slider seekBar) {
        mRopeProgressBar.defer();
    }

    @Override
    public void onTouchEnd(final Slider seekBar) {
        mRopeProgressBar.endDefer();
    }

    @Override
    public void onClick(final Component component) {
        final int progress = Integer.parseInt(mEditText.getText());
        mRopeProgressBar.animateProgress(progress);
    }

    /**
     * getLayoutId
     *
     * @return int
     */
    public abstract int getLayoutId();
}